package com.delivery.deliverysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.pojo.DO.BasicImformation;
import com.delivery.deliverysystem.pojo.DO.Product;
import com.delivery.deliverysystem.pojo.VO.ProductVO;
import com.delivery.deliverysystem.service.BasicImformationService;
import com.delivery.deliverysystem.dao.BasicImformationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【basic_imformation(基础资料表)】的数据库操作Service实现
* @createDate 2025-01-13 15:32:41
*/
@Service
@Slf4j
public class BasicImformationServiceImpl extends ServiceImpl<BasicImformationMapper, BasicImformation>
    implements BasicImformationService{
    @Autowired
    private BasicImformationMapper basicImformationMapper;

    @Override
    public Object createInformation(int type, String typeCotent) {
        BasicImformation imformation = new BasicImformation();
        QueryWrapper<BasicImformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type)
                    .eq("type_content",typeCotent);
        BasicImformation i = basicImformationMapper.selectOne(queryWrapper);
        if(i != null) return ServerResponse.createByError("内容重复，请重新操作");
        imformation.setType(type);
        imformation.setTypeContent(typeCotent);
        switch (type){
            //单位
            case 1:
                imformation.setTypeName("单位");
                break;
            //业务员
            case 2:
                imformation.setTypeName("业务员");
                break;
            //部门
            case 3:
                imformation.setTypeName("部门");
                break;
            //客户类型
            case 4:
                imformation.setTypeName("客户类型");
                break;
            //地区
            case 5:
                imformation.setTypeName("地区");
                break;
            //送货单状态
            case 6:
                imformation.setTypeName("送货单状态");
                break;
            //颜色
            case 7:
                imformation.setTypeName("颜色");
                break;
            //付款方式
            case 8:
                imformation.setTypeName("付款方式");
                break;
            //备注四
            case 9:
                imformation.setTypeName("备注四");
                break;
            //档号
            default:
                imformation.setTypeName("档号");
                break;
        }
        basicImformationMapper.insert(imformation);
        return ServerResponse.createBySuccess(imformation);
    }

    @Override
    public Object updateInformation(Integer id, String typeCotent) {
        BasicImformation imformation = basicImformationMapper.selectById(id);
        if(imformation == null) return ServerResponse.createByError("查询不到该id下的其他资料");
        QueryWrapper<BasicImformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",imformation.getType())
                .eq("type_content",typeCotent);
        BasicImformation i = basicImformationMapper.selectOne(queryWrapper);
        if(i != null) return ServerResponse.createByError("内容重复，请重新操作");
        UpdateWrapper<BasicImformation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("basic_id",id)
                     .set("type_content",typeCotent);
        basicImformationMapper.update(null,updateWrapper);
        return ServerResponse.createBySuccess("修改成功");
    }

    @Override
    public BasicImformation getInformationById(Integer id) {
        if(id == null)return null;
        BasicImformation imformation = basicImformationMapper.selectById(id);
        if(imformation != null){
            return BasicImformation.builder()
                    .basicId(imformation.getBasicId())
                    .type(imformation.getType())
                    .typeName(imformation.getTypeName())
                    .typeContent(imformation.getTypeContent())
                    .build();
        }
        return null;
    }

    @Override
    public List<BasicImformation> getInformationByType(Integer type) {
        if(type == null)return null;
        QueryWrapper<BasicImformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type);
        List<BasicImformation> imformations = basicImformationMapper.selectList(queryWrapper);
        return imformations;
    }

    @Override
    public List<BasicImformation> getCategoryList() {
        List<BasicImformation> imformations = basicImformationMapper.selectList(null);
        if(imformations!=null && !imformations.isEmpty()){
            return imformations.stream()
                    .map(imformation -> BasicImformation.builder()
                            .basicId(imformation.getBasicId())
                            .type(imformation.getType())
                            .typeName(imformation.getTypeName())
                            .typeContent(imformation.getTypeContent())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }
}




